package com.zliang;

import com.alibaba.fastjson.JSON;
import com.fasterxml.jackson.annotation.*;
import com.fasterxml.jackson.core.JsonEncoding;
import com.fasterxml.jackson.core.JsonGenerator;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.datatype.jsr310.JavaTimeModule;
import org.springframework.util.StringUtils;

import java.util.StringJoiner;

/**
 * @Author zhuangliang
 * @Date 2021/3/19
 * @Description
 * @return
 **/
public class MainStringTests {

    private static int num = 100000;
    public static final String KEY_TLIVE_ONLINE_USERS = "tlive:room:onlineUsers:%s:%s";
    private static String orgId = "orgId";
    private static String roomId = "roomId";

    public static void main(String[] args) {
        System.out.println("字符串拼接 运行次数：" + num);

        System.out.println("-----------------------------------");
        long s0 = System.currentTimeMillis();
        for (int i = 0; i < num; i++) {
            String.format(KEY_TLIVE_ONLINE_USERS,orgId, roomId);
        }
        System.out.println("String.format() 运行时长: " + (System.currentTimeMillis() - s0));
        System.out.println("-----------------------------------");

        long s1 = System.currentTimeMillis();
        for (int i = 0; i < num; i++) {
            String t = KEY_TLIVE_ONLINE_USERS+orgId+roomId;
        }
        System.out.println("+号拼接 a+b 耗时: " + (System.currentTimeMillis() - s1));
        System.out.println("-----------------------------------");

        long s2 = System.currentTimeMillis();
        for (int i = 0; i < num; i++) {
            generalKey(KEY_TLIVE_ONLINE_USERS,orgId, roomId);
        }
        System.out.println("StringBuilder sb.append(); 耗时: " + (System.currentTimeMillis() - s2));
        System.out.println("-----------------------------------");

        long s3 = System.currentTimeMillis();
        for (int i = 0; i < num; i++) {

            StringJoiner sj = new StringJoiner(":", "", "");
            sj.add(KEY_TLIVE_ONLINE_USERS).add(orgId).add(roomId);
            String desiredString = sj.toString();
        }
        System.out.println("StringJoiner 耗时: " + (System.currentTimeMillis() - s3));
        System.out.println("-----------------------------------");



    }


    public static String generalKey2(String prefix, String... values) {

        if (StringUtils.isEmpty(prefix)) {
            return null;
        } else if (prefix.endsWith(":")) {
            prefix = prefix.substring(0, prefix.lastIndexOf(":"));
        }

        if (values == null) {
            return prefix;
        }

        StringBuilder sb = new StringBuilder(prefix);
        for (String value : values) {
            sb = sb.append(":").append(value);
        }

        return sb.toString();
    }

    public static String generalKey(String prefix, String... values) {
        StringBuilder sb = new StringBuilder(prefix);
        for (String value : values) {
            sb = sb.append(":").append(value);
        }

        return sb.toString();
    }



}
